import axios from '@/utils/request'
import store from '@/store'

// 所有频道接口
export const getAllChannelAPI = () => axios({
  url: '/v1_0/channels'
})

// 用户频道接口 用户没有登录，默认返回后台设置的默认频道
export const userChannelAPI = () => axios({
  url: '/v1_0/user/channels',
  headers: {
    Authorization: 'Bearer ' + store.state.token
  }
})

// 更新用户已选频道
export const updateChannelAPI = (channels) => axios({
  url: '/v1_0/user/channels',
  method: 'PUT',
  data: {
    channels
  }
})

// 获取文章列表
export const getArticleListAPI = ({ channel_id, timestamp }) => axios({
  url: '/v1_0/articles',
  headers: {
    Authorization: 'Bearer ' + store.state.token
  },
  params: {
    channel_id,
    timestamp: timestamp || (new Date()).getTime()
  }
})

// 对文章不感兴趣
export const dislikeAPI = ({ target }) => axios({
  url: '/v1_0/article/dislikes',
  method: 'POST',
  data: {
    target: target
  }

})

// 举报文章
export const articleReportsAPI = ({ target, type, remark }) => axios({
  url: '/v1_0/article/reports',
  method: 'POST',
  data: {
    target,
    type,
    remark
  }
})

// 文章联想搜索接口
export const searchArticle = (keyWord) => axios({
  url: '/v1_0/suggestion',
  params: {
    q: keyWord
  }
})

// 文章搜索结果接口
export const searchResultAPI = ({ q, page = 1 }) => axios({
  url: '/v1_0/search',
  params: {
    q,
    page
  }
})

// 文章详情
export const articleDetailAPI = (id) => axios({
  url: `/v1_0/articles/${id}`
})

// 关注文章
export const followingArticleAPI = ({ target }) => axios({
  url: '/v1_0/user/followings',
  method: 'POST',
  data: {
    target
  }
})

// 取消关注
export const cancleFollowingArticleAPI = (uid) => axios({
  url: `/v1_0/user/followings/${uid}`,
  method: 'DELETE'
})

// 点赞-喜欢
export const likingAPI = (art_id) => axios({
  url: '/v1_0/article/likings',
  method: 'POST',
  data: {
    target: art_id
  }
})

// 取消点赞-不喜欢
export const cancleLikingAPI = (art_id) => axios({
  url: `/v1_0/article/likings/${art_id}`,
  method: 'DELETE'
})

// 获取评论列表接口
/**
 *
 * @param {
 * type:a是对文章的评论
 * source:文章id
 * offset:上一次评论列表里last_id值(上一次最后一个数据id)
 * limit:默认10条
 * }
 * @returns
 */
export const getCommentsAPI = ({ id, offset = null, limit = 10 }) => axios({
  url: '/v1_0/comments',
  params: {
    type: 'a',
    source: id,
    offset,
    limit
  }
})

// 评论点赞
export const commentsLikeAPI = (id) => axios({
  url: '/v1_0/comment/likings',
  method: 'POST',
  data: {
    target: id
  }
})

// 评论——取消点赞
export const cancelCommentAPI = (id) => axios({
  url: `/v1_0/comment/likings/${id}`,
  method: 'DELETE'
})

// 点赞——发布
export const commitCommentAPI = ({ target, content }) => axios({
  url: '/v1_0/comments',
  method: 'POST',
  data: {
    target,
    content
  }
})
